package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.movieservice.param.NameSequenceParam;
import la.iok.hzsvn.lewin.movie.entity.BookChapter;
import la.iok.hzsvn.lewin.movie.mapper.BookChapterMapper;
import la.iok.hzsvn.lewin.movie.model.BookChapterVo;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NotNullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BookChapterServiceImpl extends BaseServiceImpl<BookChapter, BookChapterMapper,Long> implements BookChapterService {
    private final MovieService movieService;
    public BookChapterServiceImpl(BookChapterMapper mapper,
                                  MovieService movieService) {
        super(mapper);
        this.movieService = movieService;
    }

    @Transactional
    @Override
    public Long save(@NotNull Token token, @NotNull Long movieId, @NotNull NameSequenceParam param) {
        String nm = ErrorCode.NAME_BLANK_ERROR.assertNotBlank(param.getName());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(param.getSequence(),"序号","序号不能为空");
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(param.getSequence() > 0,"序号","序号必须大于0");
        movieService.validate(token,movieId);
        BookChapter entity = new BookChapter();
        entity.setMovieId(movieId);
        entity.setName(nm);
        entity.setSequence(param.getSequence());
        insertSelective(entity);
        return entity.getId();
    }

    @Transactional
    @Override
    public void update(@NotNull Token token, @NotNull Long id, @NotNull NameSequenceParam param) {
        String nm = ErrorCode.NAME_BLANK_ERROR.assertNotBlank(param.getName());
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(param.getSequence(),"序号","序号不能为空");
        ErrorCode.PROPERTY_CHECK_ERROR.assertTrue(param.getSequence() > 0,"序号","序号必须大于0");
        BookChapter entity = select(id);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(entity,id);
        movieService.validate(token,entity.getMovieId());
        update(id,"name",nm);
        update(id,"sequence",param.getSequence());
    }

    @Transactional
    @Override
    public void delete(@NotNull Token token, @NotNull Long id) {
        BookChapter entity = select(id);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(entity,id);
        movieService.validate(token,entity.getMovieId());
        delete(id);
    }

    @NotNull
    @NotNullElement
    @Override
    public List<BookChapterVo> movieChapters(@NotNull Token token,@Nullable Long movieId) {
        if(movieId == null){
            return Collections.emptyList();
        }
        movieService.validate(token,movieId);
        List<BookChapter> list = listByProperty("movieId",movieId,"sequence");
        return list.stream().map(entity->{
            BookChapterVo vo = new BookChapterVo();
            BeanUtils.copyProperties(entity,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @NotNull
    @NotNullElement
    @Override
    public List<BookChapter> movieChapters(@Nullable Long movieId) {
        if(movieId == null){
            return Collections.emptyList();
        }
        return listByProperty("movieId",movieId,"sequence");
    }
}
